package com.jsh.erp.service.contract;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.jsh.erp.datasource.entities.Contract;
import com.jsh.erp.datasource.entities.ContractExample;
import com.jsh.erp.service.ICommonQuery;
import com.jsh.erp.service.project.ProjectService;
import com.jsh.erp.service.supplier.SupplierService;
import com.jsh.erp.utils.Constants;
import com.jsh.erp.utils.QueryUtils;
import com.jsh.erp.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service(value = "contract_component")
@ContractResource
public class ContractComponent implements ICommonQuery {

    @Resource
    private ContractService contractService;
    @Resource
    private ProjectService projectService;
    @Resource
    private SupplierService supplierService;

    @Override
    public Object selectOne(Long id) throws Exception {
        return contractService.getContract(id);
    }

    @Override
    public List<?> select(Map<String, String> map)throws Exception {
        return  getProjectList(map);
    }

    private List<?> getProjectList(Map<String, String> map)throws Exception {
        String search = map.get(Constants.SEARCH);
        String name = StringUtil.getInfo(search, "name");
        String type = StringUtil.getInfo(search, "type");
        String projectId = StringUtil.getInfo(search, "projectId");
        String sn = StringUtil.getInfo(search, "sn");
        String roleType = StringUtil.getInfo(search, "roleType");
        return contractService.select(name, type, projectId, sn, roleType, QueryUtils.offset(map), QueryUtils.rows(map));
    }

    @Override
    public Long counts(Map<String, String> map)throws Exception {
        String search = map.get(Constants.SEARCH);
        String name = StringUtil.getInfo(search, "name");
        String type = StringUtil.getInfo(search, "type");
        String projectId = StringUtil.getInfo(search, "projectId");
        String sn = StringUtil.getInfo(search, "sn");
        String roleType = StringUtil.getInfo(search, "roleType");
        return contractService.countContract(name, type, projectId, sn, roleType);
    }

    @Override
    public int insert(JSONObject obj, HttpServletRequest request)throws Exception {
        return contractService.insertContract(obj, request);
    }

    @Override
    public int update(JSONObject obj, HttpServletRequest request)throws Exception {
        return contractService.updateContract(obj, request);
    }

    @Override
    public int delete(Long id, HttpServletRequest request)throws Exception {
        return contractService.deleteContract(id, request);
    }

    @Override
    public int deleteBatch(String ids, HttpServletRequest request)throws Exception {
        return contractService.batchDeleteContract(ids, request);
    }

    @Override
    public int checkIsNameExist(Long id, String name)throws Exception {
        return contractService.checkIsNameExist(id, name);
    }

}
